package com.collectplus.express.parcel.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;

import com.collectplus.express.MainActivity;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.ParcelReceiveReachedStationBean;
import com.collectplus.express.parcel.ParcelReceiveAdapter;

/**
 * 已到驿站的包裹
 *
 * @author kaka<br>
 *         2017-1-10上午11:03:04
 */
public class ParcelReceiveWaitFragment extends BaseReceiverParcelFragment {

    private ParcelReceiveAdapter mAdapter;
    private ParcelReceiveReachedStationBean stationBean = null;
    private String takeId = "";
    private boolean isRequest = true;

    @Override
    protected void findViewById() {
        setContentView(R.layout.parcel_receive_list);
        super.findViewById();
        mAdapter = new ParcelReceiveAdapter(null, this);
        mListView.setAdapter(mAdapter);

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().getReceiverReachedStationParcelList();
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.receiver_parcel_reached_station_list:
                onRefreshComplete();
                cancelLoadingDialog();
                final AppResult<ParcelReceiveReachedStationBean> result = JSON.parseList(msg.obj, ParcelReceiveReachedStationBean.class);
                if (result.isSuccess()) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mAdapter == null) {
                                mAdapter = new ParcelReceiveAdapter(result.getResultList(), ParcelReceiveWaitFragment.this);
                                mListView.setAdapter(mAdapter);
                            } else {
                                mAdapter.setItems(result.getResultList(), true);
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            //揽收包裹成功后刷新适配器
            case MsgID.receiver_parcel_pick_up_data:
                cancelLoadingDialog();
                final AppResult<?> appResult = JSON.parser(msg.obj);
                getActivityContext().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (appResult.isSuccess()) {
                            Intent intent = new Intent();
                            intent.setClass(getActivityContext(), MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            Bundle bundle = new Bundle();
                            bundle.putString("stationId", stationBean.getId());
                            bundle.putString("takeId", takeId);
                            intent.putExtras(bundle);
                            startActivity(intent);
                            getActivityContext().finish();
                        } else {
                            showToast(appResult.getMessage());
                        }
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    public void pickUpParcel(ParcelReceiveReachedStationBean stationBean, int position) {
        this.stationBean = stationBean;
        if (stationBean.getParcels().size() > 0) {
            takeId = stationBean.getParcels().get(0).getTakeId();
        } else {
            takeId = "";
        }
        showLoadingDialog(null);
        AppHttp.getInstance().receiverPickUpParcelList(stationBean.getId());
    }

    @Override
    public void onStop() {
        super.onStop();
        isRequest = true;
    }

    @Override
    public void onResume() {
        if (isRequest) {
            showLoadingDialog(null);
            AppHttp.getInstance().getReceiverReachedStationParcelList();
        }
        isRequest = false;
        super.onResume();
    }
}
